package cn.jianyongjie.org.controller;

import cn.jianyongjie.basic.aoon.PreAuthorize;
import cn.jianyongjie.basic.exception.CustomException;
import cn.jianyongjie.basic.utils.ExcelUtil;
import cn.jianyongjie.basic.vo.EcharsShopVo;
import cn.jianyongjie.org.domain.ShopAuditLog;
import cn.jianyongjie.org.service.IShopService;
import cn.jianyongjie.org.domain.Shop;
import cn.jianyongjie.org.query.ShopQuery;
import cn.jianyongjie.basic.utils.AjaxResult;
import cn.jianyongjie.basic.utils.QueryResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    public IShopService shopService;


    /*
    * @Title: ShopController
    * @Description: 审核通过接口
    * @Author: JianYongJie
    * @Version: 1.0
    * @Date:  2022/5/6 12:37
    * @Parameters: * @param shopAuditLog
    * @Return cn.jianyongjie.basic.utils.AjaxResult
    */
    @PostMapping("/audit/pass")
    public AjaxResult pass(@RequestBody ShopAuditLog shopAuditLog){
        try {
            shopService.pass(shopAuditLog);
            return AjaxResult.success();
        }catch (CustomException e){
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    /*
    * @Title: ShopController
    * @Description: 审核驳回接口
    * @Author: JianYongJie
    * @Version: 1.0
    * @Date:  2022/5/6 12:38
    * @Parameters: * @param shopAuditLog
    * @Return cn.jianyongjie.basic.utils.AjaxResult
    */
    @PostMapping("/audit/reject")
    public AjaxResult reject(@RequestBody ShopAuditLog shopAuditLog){
        try {
            shopService.reject(shopAuditLog);
            return AjaxResult.success();
        }catch (CustomException e){
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    /*
    * @Title: ShopController
    * @Description: 分组查询状态条数
    * @Author: JianYongJie
    * @Version: 1.0
    * @Date:  2022/5/5 21:21
    * @Parameters: * @param
    * @Return java.util.List<java.lang.Long>
    */
    @GetMapping("/echars")
    public HashMap<String, Object> findShopEchars(){
        // 获取分组查询状态条数
        List<EcharsShopVo> shopStatistics = shopService.findShopStatistics();
        // 创建两个int和long的list对象
        ArrayList<Integer> states = new ArrayList<>();
        ArrayList<Long> nums = new ArrayList<>();
        // 循环遍历数据
        for (EcharsShopVo shopStatistic : shopStatistics) {
            states.add(shopStatistic.getState());
            nums.add(shopStatistic.getNum());
        }

        // 创建一个map对象
        HashMap<String, Object> map = new HashMap<>();
        // 将数据设置进map
        map.put("x",states);
        map.put("y",nums);

        // 最后返回map数据
        return map;
    }


    /*
    * @Title: ShopController
    * @Description: 导出表数据
    * @Author: JianYongJie
    * @Version: 1.0
    * @Date:  2022/5/5 16:01
    * @Parameters: * @param response
    * @Return void
    */
    @GetMapping("/export")
    public void exportData(HttpServletResponse response){
        try {
            // 先查询所有数据
            List<Shop> shops = shopService.loadAll();
            // 调用工具类设置参数
            ExcelUtil.exportExcel(shops,null,"店铺表",Shop.class,"shop.xlsx",response);
        }catch (Exception e){
            e.printStackTrace();
        }

    }


    /*
    * @Title: ShopController
    * @Description: 导入数据
    * @Author: JianYongJie
    * @Version: 1.0
    * @Date:  2022/5/5 16:02
    * @Parameters: * @param response
    * @Return void
    */
    @PostMapping("/import/excel")
    public void excel(@RequestPart("file") MultipartFile file){
        // 解析数据
        List<Shop> shops = ExcelUtil.importExcel(file, 0, 1, Shop.class);
        // 批量新增
        shopService.addShops(shops);
    }





    /*
    * @Title: ShopController
    * @Description: 店铺入驻
    * @Author: JianYongJie
    * @Version: 1.0
    * @Date:  2022/5/9 8:45
    * @Parameters: * @param shop
    * @Return cn.jianyongjie.basic.utils.AjaxResult
    */
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop){
        try {
            shopService.settlement(shop);
            System.out.println(shop.getAdmin().getAge());
            return AjaxResult.success();
        }catch (CustomException e){
            e.printStackTrace();
            return AjaxResult.error().setMessage(e.getMessage());
        }
    }


    /**
     * 保存和修改公用的
     * @param shop  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null){
                // 设置时间
                shop.setRegisterTime(new Date());
                // 将修改过后的状态改为审核通过
                shop.setState(1);
                // 调用修改
                shopService.update(shop);
            }

            else
                shopService.save(shop);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    @PreAuthorize(name = "根据id删除数据",value = "shop:Delete")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.remove(id);
            return new AjaxResult();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.error();
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Shop get(@PathVariable("id")Long id)
    {
        return shopService.loadById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Shop> list(){

        return shopService.loadAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return QueryResult 分页对象
    */
    @PostMapping("/list")
    public QueryResult<Shop> json(@RequestBody ShopQuery query)
    {

        return shopService.queryData(query);
    }
}
